﻿// Search portfolio
Ext.define('Cps.view.dashboard.popup.Search', {
	extend: 'Ext.window.Window',
	alias: 'widget.Search',

	height: 500,
  width: 800,
	title: Cps.Locale.Search.title1,
	modal: true,
	closable: true,
	resizable: false,
	layout: {type: 'vbox', align: 'stretch'},
	items: [
		{
			xtype: 'form',
			bodyPadding: '7px 20px',
			layout: 'hbox',
			items: [
				{
					xtype: 'textfield',
					id: 'txtsearchTag1',
					width: 466,
					//flex: 3,
					labelWidth: 70,
					margin: '0 4 0 0',
					fieldLabel: Cps.Locale.Search.field,
					listeners: {
		        specialkey: function(field, e){
		          if (e.getKey() == e.ENTER) {
								var gridPortfolio = Ext.getCmp('gridPortfolio');
								Ext.Ajax.request({
									method: 'GET',
									url: Cps.Config.Api.getPortfolio,
									params: function() {
										var value = getParam(), txtsearchTag1 = Ext.getCmp('txtsearchTag1');
										value.searchTag = txtsearchTag1.getValue();
										value.isActive = true;
										return value;
									},
									success: function(data) {
										if (IsJson(data.responseText))
										{
											data = Ext.JSON.decode(data.responseText).data;
											data = data ? data.ttPortfolio : null;
											data = data ? data.ttPortfolioRow : null;
											var f_data = [];
											if (data != undefined)
											{
												if (data.length != undefined)
												{
													for(i = 0; i < data.length; i++) {
														f_data.push(data[i]);
													}
												}
												else
													f_data.push(data);
											}
											gridPortfolio.store.loadData(f_data);
											gridPortfolio.getView().focusRow(0);
										}
									},
									scope: this
								});
							}
						}
					}
				},
				{
					xtype: 'button',
					width: 120,
					//flex: 1,
					text: Cps.Locale.Search.btn1,
					handler: function() {
						var gridPortfolio = Ext.getCmp('gridPortfolio');
						Ext.Ajax.request({
							method: 'GET',
							url: Cps.Config.Api.getPortfolio,
							params: function() {
								var value = getParam(), txtsearchTag1 = Ext.getCmp('txtsearchTag1');
								value.searchTag = txtsearchTag1.getValue();
								value.isActive = true;
								return value;
							},
							success: function(data) {
								if (IsJson(data.responseText))
								{
									data = Ext.JSON.decode(data.responseText).data;
									data = data ? data.ttPortfolio : null;
									data = data ? data.ttPortfolioRow : null;
									var f_data = [];
									if (data != undefined)
									{
										if (data.length != undefined)
										{
											for(i = 0; i < data.length; i++) {
												f_data.push(data[i]);
											}
										}
										else
											f_data.push(data);
									}
									gridPortfolio.store.loadData(f_data);
									gridPortfolio.getView().focusRow(0);
								}
							},
							scope: this
						});
					}
				}
			]
		},
		{
			xtype: 'grid',
			id: 'gridPortfolio',
			flex: 2,
			store: Ext.create('Ext.data.Store', {
				fields: [
					{ name: 'PortCode', type: 'int' },
					{ name: 'PortName', type: 'string' }
				],
				data: []
			}),
			columns: {
				items: [
					{
						text: Cps.Locale.Search.column1,
						width: 100,
						hideable: false,
						dataIndex: 'PortCode',
						renderer: function(data, meta, rec, row, col) {
							if(!data)
								data = '';
							else{
								str = Ext.util.Format.number(data, '0,000');
								return str;
							}
							return data;
						}
					},
					{
						text: Cps.Locale.Search.column2,
						flex: 2,
						hideable: false,
						dataIndex: 'PortName'
					}
				]
			},
			listeners: {
				itemdblclick: function( grid, record, item, index, e, eOpts ){
						var txtPortfolioCode = Ext.getCmp('txtPortfolioCode'),
						txtPortfolioName = Ext.getCmp('txtPortfolioName'),
						gridPortfolio = Ext.getCmp('gridPortfolio'), 
						select;
						if (gridPortfolio.getSelectionModel().getSelection() != "")
						{
							select = gridPortfolio.getSelectionModel().getSelection();
							var portcode = select[0].data.PortCode;
							portcode = Ext.util.Format.number(portcode, '0,000');
							portcode = portcode.replace(/[,|.]/g,"'");
							txtPortfolioCode.setValue(portcode);
							txtPortfolioName.setValue(select[0].data.PortName);
						}
						var win= gridPortfolio.up('window');
						win.close();
				}
			}
		}
	],
	buttons: [
		{
			text: Cps.Locale.Search.btn2,
			handler: function() {
				var txtPortfolioCode = Ext.getCmp('txtPortfolioCode'),
				txtPortfolioName = Ext.getCmp('txtPortfolioName'),
				gridPortfolio = Ext.getCmp('gridPortfolio'), 
				select;
				if (gridPortfolio.getSelectionModel().getSelection() != "")
				{
					select = gridPortfolio.getSelectionModel().getSelection();
					var portcode = select[0].data.PortCode;
					portcode = Ext.util.Format.number(portcode, '0,000');
					portcode = portcode.replace(/[,|.]/g,"'");
					txtPortfolioCode.setValue(portcode);
					txtPortfolioName.setValue(select[0].data.PortName);
				}
				var win= gridPortfolio.up('window');
				win.close();
			}
		},
		{
			text: Cps.Locale.Search.btn3,
			handler: function() {
				this.ownerCt.ownerCt.close();
			}
		}
	],
	listeners:{
    close:function( panel, eOpts ){
    	var grid= panel.down('grid');
			var store= grid.getStore();
			store.removeAll();
    }
  }
});
function loadData(){
	var gridPortfolio = Ext.getCmp('gridPortfolio');
	Ext.Ajax.request({
		method: 'GET',
		url: Cps.Config.Api.getPortfolio,
		params: function() {
			var value = getParam(), txtsearchTag1 = Ext.getCmp('txtsearchTag1');
			value.searchTag = txtsearchTag1.getValue();
			value.isActive = true;
			return value;
		},
		success: function(data) {
			if (IsJson(data.responseText))
			{
				data = Ext.JSON.decode(data.responseText).data;
				data = data ? data.ttPortfolio : null;
				data = data ? data.ttPortfolioRow : null;
				var f_data = [];
				if (data != undefined)
				{
					if (data.length != undefined)
					{
						for(i = 0; i < data.length; i++) {
							f_data.push(data[i]);
						}
					}
					else
						f_data.push(data);
				}
				gridPortfolio.store.loadData(f_data);
				gridPortfolio.getView().focusRow(0);
			}
		},
		scope: this
	});
}
